<template>
  <div class="main" style="margin: 10px">
    <el-card>
      <div>
        <!--工具栏-->
        <div class="head-container">
          <div>
            <!-- 搜索 -->
            <span>
              <el-input v-model="searchKey" clearable placeholder="请输入用户名" style="width: 200px" class="filter-item" @keyup.enter.native="toQuery" />
              <span class="filter-item">
                <el-button type="success" icon="el-icon-search" @click="searchFilter">搜索</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
              </span>
            </span>
            <span>
              <el-button-group>
                <el-button icon="el-icon-search"></el-button>
                <el-button icon="el-icon-refresh"></el-button>
              </el-button-group>
            </span>
          </div>
        </div>
      </div>

      <el-card style="margin-top: 10px">
        <el-table
          ref="table"
          :data="tableData"
          style="width: 100%"
          row-key="id"
          lazy
          >>
          <el-table-column type="index" label="#" width="55"> </el-table-column>
          <el-table-column prop="roleName" label="用户名"> </el-table-column>
          <el-table-column prop="state" label="状态" align="center">
            <template slot-scope="scope">
              <el-switch
                @change="changeSwitch(scope.row)"
                v-model="scope.row.state"
                :active-value="1"
                active-text="正常"
                :inactive-value="2"
                inactive-text="禁用"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="description" label="描述"> </el-table-column>
          <el-table-column prop="updateTime" label="修改日期" align="center">
            <template slot-scope="scope">
              {{ scope.row.updateTime | formatDate }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建日期" align="center">
            <template slot-scope="scope">
              {{ scope.row.createTime | formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="300" align="center">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button type="primary" icon="el-icon-share" @click="handlePower(scope.row)">权限</el-button>
              <el-button type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      </el-card>
    <!-- </el-card> -->
    <!-- 分页 -->
    <pagination :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList"/>
    <DialogMenu :dialogMenu="dialogMenu" :formData="formData" @handleSubmit="handleSubmit" @handleSubmitEdit="handleSubmitEdit" ></DialogMenu>
    <PermissionsDialog ref="child" @giveRules='giveRules' @resetDefaultKey='resetDefaultKey' :permissionsDialog='permissionsDialog' :defaultkey='defaultkey'></PermissionsDialog>
  </div>
</template>

<script>
import { getRolePageList,updateRoleState, roleAdd, updateRole, deleteRole, authorize, getPowerList } from "@/api/system/role";
import Pagination from "@/components/Pagination"; // el分页
import DialogMenu from "@/components/System/role/dialogRole.vue";
import PermissionsDialog from "@/components/System/role/permissionsDialog.vue";

export default {
  data() {
    return {
      AddBtnAuth: ["add"],
      dialogMenu: {
        show: false,
      },
      permissionsDialog: {
        show: false,
        id: ""
      },
      defaultkey: [],
      //条件查询搜索
      total: 0,
      searchKey: '',
      tableData: [],
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        roleName: undefined,
      },
      formData: {},
      roleState: {},
      rolePower: {},
    };
  },

  methods:{
    getList() {
      getRolePageList(this.listQuery).then((res) => {
        this.tableData = res.result.data.list;
        this.total = res.result.data.total;
        this.listQuery.pageNum = res.result.data.pageNum;
        this.listQuery.pageSize = res.result.data.pageSize;

        setTimeout(() => {
          this.listLoading = false;
        }, 1.5 * 1000);
      });
    },
    //监听switch事件
    async changeSwitch(row) {
      this.roleState.rid = row.rid;
      this.roleState.state = row.state;
      updateRoleState(this.roleState).then(res => {
        if (res.code == 20000) {
            this.$message({
              type: "success",
              message: "修改成功!",
            });
        } else{
            this.$message({
            type: "info",
            message: "修改失败！",
          });
        }
      })
    },
    //条件搜索
    searchFilter() {
      this.listQuery.roleName = this.searchKey;
      this.getList();
    },
    //按快捷键
    toQuery() {
      this.searchFilter();
    },
    // 新增按钮触发
    handleAdd() {
      this.dialogMenu = {
        show: true,
        title: "新增菜单",
        option: "add",
      };
      this.formData = {
        roleName: "",
        state: 1,
        description: null,
      };
    },
    //提交新增请求
    handleSubmit() {
      roleAdd(this.formData).then((res) => {
        if (res.code == 20000) {
          if (res.result.data == true) {
            this.dialogMenu.show = false;
            this.$message.success("新增成功！");

            this.getList();
          } else {
            this.$message({
              type: "info",
              message: "修改失败！",
            });
          }
        } else {
          this.dialogMenu.show = false;
          this.$message.success(res.message);
        }
      });
    },
    // 编辑按钮触发
    handleEdit(row) {
      this.dialogMenu = {
        show: true,
        title: "修改菜单",
        option: "edit",
      };
      this.formData = {
        rId: row.rid,
        roleName: row.roleName,
        state: row.state,
        description: row.description,
      };
    },
    // 提交编辑请求
    handleSubmitEdit() {
      updateRole(this.formData).then(res => {
        if (res.code == 20000) {
          if (res.result.data == true) {
            this.dialogMenu.show = false
            this.$message.success('修改成功！')
            this.getList()
          } else {
            this.$message({
              type: "info",
              message: "修改失败！",
            });
          }
        } else {
          this.$message({
            type: "info",
            message: "修改失败！",
          });
        }
      })
    },
    // 删除按钮触发
    handleDelete(row) {
      this.$confirm(
        "此操作将永久删除该角色，是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          deleteRole(row.rid).then((res) => {
            if (res.code == 20000) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            }

          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //权限分配
    handlePower(row) {
      this.permissionsDialog = {
        show: true,
        rId: row.rid
      }

      this.$refs.child.isCheck = true
      getPowerList(this.permissionsDialog.rId).then(res => {

        this.defaultkey = res.result.data
      })
      //this.defaultkey = arr
      setTimeout(() => {
        this.$refs.child.isCheck = false
      }, 500);
    },
    resetDefaultKey() {
      this.defaultkey = []
    },
    giveRules(data) {
      this.rolePower = {
        rId: this.permissionsDialog.rId,
        rules: data,
      };
      authorize(this.rolePower).then(res => {
        this.$notify({
          title: '成功',
          message: '分配成功！',
          type: 'success'
        });
        this.permissionsDialog.show = false
        this.getList();
      })
    }
  },
  mounted() {
    this.getList();
  },
  components: {
    Pagination,
    DialogMenu,
    PermissionsDialog
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-switch.is-disabled {
  opacity: 1;
}
::v-deep .el-switch.is-checked .el-switch__core {
  &:hover {
    cursor: pointer;
  }
}
// 工具栏
.head {
  &-container {
    text-indent: -10px;
    > div {
      display: flex;
      align-items: center;
      justify-content: space-between;
      > span {
        display: flex;
        align-items: center;
      }
      .filter {
        &-item {
          margin-left: 10px;
        }
      }
    }
  }
}
</style>
